#include<stdio.h>
#include<stdlib.h>
#include "flash.h"
#include "savedata.h"
#include "MyFlash.h"
#include "include.h"



unsigned char Dflash_Write_Flag=0;
#define First_Time_Init 33
void MyFlash_Write(void)
{
  if(Dflash_Write_Flag==1)
  {
      Dflash_Write_Flag=0;
      DisableInterrupts;
      flash_erase_sector(SECTOR_NUM);
      DFlash_Write_Int(SECTOR_NUM,0,First_Time_Init);              
      
      DFlash_Write_Float(SECTOR_NUM,1,speed_max);
      DFlash_Write_Float(SECTOR_NUM,2,bound_turnner_1);  
      DFlash_Write_Float(SECTOR_NUM,3,bound_turnner_2);  
      DFlash_Write_Float(SECTOR_NUM,4,I_max);
      DFlash_Write_Float(SECTOR_NUM,5,turn_P );
      DFlash_Write_Float(SECTOR_NUM,6,turn_D);
      DFlash_Write_Float(SECTOR_NUM,7,k_mid);      
      DFlash_Write_Float(SECTOR_NUM,8,run_time);
      DFlash_Write_Float(SECTOR_NUM,9,bound_cross1);
      DFlash_Write_Float(SECTOR_NUM,10,bound_cross2);
      DFlash_Write_Float(SECTOR_NUM,11,speed_P);
      DFlash_Write_Float(SECTOR_NUM,12,speed_I);
      DFlash_Write_Float(SECTOR_NUM,13,zhi_speed);
      DFlash_Write_Float(SECTOR_NUM,14,k_speed);
      DFlash_Write_Float(SECTOR_NUM,15,least_speed_max);
      DFlash_Write_Float(SECTOR_NUM,16,speed_P);
      DFlash_Write_Float(SECTOR_NUM,17,speed_I);
      DFlash_Write_Float(SECTOR_NUM,18,e_error_zero);
      
      flash_write(SECTOR_NUM,flag_place,saved_flag);
      EnableInterrupts;
  }
  
}
void MyFlash_Read(void)
{
  int FirstStart=0;
  FirstStart=DFlash_Read_Int(SECTOR_NUM,0);
   if(FirstStart==First_Time_Init)
   {
   
      speed_max =DFlash_Read_Float(SECTOR_NUM,1);
      bound_turnner_1=DFlash_Read_Float(SECTOR_NUM,2);   
      bound_turnner_2=DFlash_Read_Float(SECTOR_NUM,3);   
      I_max=DFlash_Read_Float(SECTOR_NUM,4);
      turn_P=DFlash_Read_Float(SECTOR_NUM,5);
      turn_D=DFlash_Read_Float(SECTOR_NUM,6);
      k_mid=DFlash_Read_Float(SECTOR_NUM,7);      
      run_time=DFlash_Read_Float(SECTOR_NUM,8);
      bound_cross1=DFlash_Read_Float(SECTOR_NUM,9);
      bound_cross2=DFlash_Read_Float(SECTOR_NUM,10);
      speed_P=DFlash_Read_Float(SECTOR_NUM,11);
      speed_I=DFlash_Read_Float(SECTOR_NUM,12);
      zhi_speed=DFlash_Read_Float(SECTOR_NUM,13);
      k_speed=DFlash_Read_Float(SECTOR_NUM,14);
      least_speed_max=DFlash_Read_Float(SECTOR_NUM,15);
      speed_P=DFlash_Read_Float(SECTOR_NUM,16);
      speed_I=DFlash_Read_Float(SECTOR_NUM,17);
      e_error_zero=DFlash_Read_Float(SECTOR_NUM,18);
   }
  
}


void Save_Specific(int order)
{
  switch(order)
  {
  case 1:
    SECTOR_NUM=Parameter_One;
    break;
  case 2:
    SECTOR_NUM=Parameter_Two;
    break;
  case 3:    
    SECTOR_NUM=Parameter_Three;
    break;
  case 4:
    SECTOR_NUM=Parameter_Four;
    break;
  case 5:
    SECTOR_NUM=Parameter_Five;
    break;
  case 6:    
    SECTOR_NUM=Parameter_Six;
    break;
  case 7:
    SECTOR_NUM=Parameter_Seven;
    break;
  default:
    return;
    break;
  }
  Dflash_Write_Flag=1;
  MyFlash_Write();
  Dflash_Write_Flag=0;
  SECTOR_NUM=Parameter_Block;
}

void Read_Specific(int order)
{
  switch(order)
  {
  case 1:
    SECTOR_NUM=Parameter_One;
    break;
  case 2:
    SECTOR_NUM=Parameter_Two;
    break;
  case 3:    
    SECTOR_NUM=Parameter_Three;
    break;
  case 4:
    SECTOR_NUM=Parameter_Four;
    break;
  case 5:
    SECTOR_NUM=Parameter_Five;
    break;
  case 6:    
    SECTOR_NUM=Parameter_Six;
    break;
  case 7:
    SECTOR_NUM=Parameter_Seven;
    break;
  default:
    return;
    break;
    
  }
  MyFlash_Read();
  SECTOR_NUM=Parameter_Block;
  Dflash_Write_Flag=1;
  MyFlash_Write();
  Dflash_Write_Flag=0;
  
}


void Write_Track(float a[],float b[],float c[],int n,int block_num)
{
  int i;
  uint16 Track_Block;
  switch(block_num)
  {
  case 1:
    Track_Block=Track_Block1;
    break;
  case 2:
    Track_Block=Track_Block2;
    break;
  case 3:
    Track_Block=Track_Block3;
    break;
  default:
    return;
    break;
  }
  DisableInterrupts;
  flash_erase_sector(Track_Block);
  for(i=0;i<n;i++)
  {
    DFlash_Write_Float(Track_Block,i,a[i]);  
  }
  for(i=0;i<n;i++)
  {
    DFlash_Write_Float(Track_Block,n+i,b[i]);  
  }
  for(i=0;i<n;i++)
  {
    DFlash_Write_Float(Track_Block,2*n+i,c[i]);  
  }
  flash_write(Track_Block,3*n*4,saved_flag);
  EnableInterrupts;
  
}

void Read_Track(float a[],float b[],float c[],int n,int block_num)
{
  int i;
  uint16 Track_Block;
  switch(block_num)
  {
  case 1:
    Track_Block=Track_Block1;
    break;
  case 2:
    Track_Block=Track_Block2;
    break;
  case 3:
    Track_Block=Track_Block3;
    break;
  default:
    return;
    break;
  }
  DisableInterrupts;
  
  for(i=0;i<n;i++)
  {
    a[i]=DFlash_Read_Float(Track_Block,i);  
  }
  for(i=0;i<n;i++)
  {
    b[i]=DFlash_Read_Float(Track_Block,n+i);  
  }
  for(i=0;i<n;i++)
  {
    c[i]=DFlash_Read_Float(Track_Block,2*n+i);  
  }
  
  EnableInterrupts;
  
}